-- Stone
-- Created by chengb Feb/3/2016
-- 石柱格子

require "game/ui/form/stone/UIMagicStone"

-- 初始化
function initStone(self)
    self.item:removeAllChildren();

    local function onClicked(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            if self.gridData.state == GRID_STATE_DISABLE then
                -- 已经失效，不处理
                return;
            end

            -- 更新建筑访问
            self:updateBuildingVisit();

            -- 格子不可到达
            if not self.gridData:canHit() then
                alert(getLocStr("grid_unreachable"));
                return;
            end

            local pos = self.gridData:getPos();

            -- 打开石柱界面
            UIMgr.getCurrentScene():removeFormByName("UIMagicStone");
            local uiForm = UIMagicStone.create(pos);
            UIMgr.getCurrentScene():addForm(uiForm);

            -- 音效
            AudioM.playFx("ui_open1");
        end
    end

    local stoneId = self.gridData.id;
    local stoneType = MagicStoneM.query(stoneId, "type");

    -- 用一张透明的图片来接收点击事件
    local iconName = MagicStoneM.getStoneIcon(stoneId);
    local iconPath = getLevelItemIconPath(iconName);
    self.icon = ccui.ImageView:create(iconPath);
    self.icon:setTouchEnabled(true);
    self.icon:addTouchEventListener(onClicked);
    self.item:addChild(self.icon);
    self.icon:setOpacity(0);

    -- 创建石柱模型，初始停在第一帧
    local effectId = iif(stoneType == STONE_TYPE_LIGHT, 1197, 1192);
    local animation = createEffectAnim(effectId);
    animation:gotoFrameAndPause(0);
    self.item.animation = animation;
    self.item:addChild(animation);

    self.item:setPositionY(10);
end

-- 关注石柱关闭的事件
function onCloseStone(self)
    -- 播放石柱关闭特效
    playStoneCloseEffect(self);
end

-- 关注学习石柱技能的事件
function onStudyStoneSkill(self)
    local function disappear()
        playStoneDisappear(self);
    end

    -- 播放石柱消失特效
    performWithDelay(self, disappear, 0.5);
end
